package com.appspot.diasporajava.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.appspot.diasporajava.dao.PodDao;
import com.appspot.diasporajava.dao.UserDao;
import com.appspot.diasporajava.entities.Pod;
import com.appspot.diasporajava.entities.Post;
import com.appspot.diasporajava.entities.User;
import com.appspot.diasporajava.federation.PublicFeed;
import com.appspot.diasporajava.form.AddUserForm;
import com.appspot.diasporajava.manager.FederationManager;
import com.appspot.diasporajava.manager.FeedManager;

@Controller
public class MainController {
	private Log log = LogFactory.getLog(MainController.class);

	@Autowired
	PublicFeed publicFeed;

	@Autowired
	PodDao<Pod,Long> podDao;

	@Autowired
	FederationManager federationManager;

	@Autowired
	UserDao<User,Long> userDao;

	@Autowired
	FeedManager feedManager;

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		log.info("Returning hello view");

		return new ModelAndView("hello.jsp");
	}

	@RequestMapping({ "/index.html" })
	public ModelAndView showHomePage() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("hello");
		return modelAndView;
	}

	@RequestMapping({ "/public" })
	public ModelAndView showPublic() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("public_feed");
		modelAndView.addObject("feed", publicFeed.getContentList("https://joindiaspora.com/public/u_g_g_l_y.atom"));
		return modelAndView;
	}

	@RequestMapping({ "/users" })
	public ModelAndView users() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("users");
		modelAndView.addObject("users", userDao.list());
		modelAndView.addObject(new AddUserForm());
		return modelAndView;
	}

	@RequestMapping({ "/feed/{userId}" })
	public ModelAndView feed(@PathVariable String userId) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("public_feed");
		//Long id = Long.getLong(userId);
		Long id=Long.parseLong(userId);
		// User user=userDao.find(id);
		//User user = userDao.find(new Key(userId));
		List<Post> posts = feedManager.getFeeds(id);
		// feedManager.transform(posts);
		modelAndView.addObject("feed", posts);
		return modelAndView;
	}

	@RequestMapping({ "/pods" })
	public ModelAndView pods() {
		List<Pod> pods = podDao.list();

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("pods", pods);
		modelAndView.addObject(new Pod());
		return modelAndView;
	}

	@RequestMapping(value = "/newuser", method = RequestMethod.POST)
	public String newUser(AddUserForm addUserForm, BindingResult bindingResult) {
		log.debug(addUserForm);
		try {
			federationManager.findUser(addUserForm.getEmail());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "users";
	}

	@RequestMapping(value = "/newpod", method = RequestMethod.POST)
	public ModelAndView newPod(Pod pod, BindingResult bindingResult) {
		podDao.create(pod);

		return pods();
	}

}